package com.cangmaomao.m_mePen.adapter

import android.annotation.SuppressLint
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.cangmaomao.lib.utils.FileUtils.delDir
import com.cangmaomao.lib.utils.FileUtils.delFolder
import com.cangmaomao.lib.utils.FileUtils.updateFileName
import com.cangmaomao.lib.utils.toast
import com.cangmaomao.m_mePen.R
import com.cangmaomao.m_mePen.bean.FileListBean
import com.cangmaomao.recyclerview.adapter.CMMAdapter
import com.cangmaomao.recyclerview.adapter.CMMViewHolder

class FileListAdapter(layoutId: Int = R.layout.item_file_list) : CMMAdapter<FileListBean>(layoutId) {

    @SuppressLint("SetTextI18n")
    override fun convert(holder: CMMViewHolder, data: FileListBean, p2: Int) {
        val file_Name = holder.getView<EditText>(R.id.fileName)
        holder.setText(R.id.fileName, data.fileName)
        holder.setText(R.id.fileNum, "${data.fileNum}个文件")
        holder.getView<LinearLayout>(R.id.ll_layout).visibility = if (data.isEdit) View.VISIBLE else View.GONE
        holder.getView<TextView>(R.id.fileNum).visibility = if (data.isEdit) View.GONE else View.VISIBLE

        val update = holder.getView<TextView>(R.id.update)
        update.setOnClickListener {
            update.text = "保存"
            file_Name.isEnabled = true
            update.setOnClickListener {
                val c = updateFileName(data.path, file_Name.text.toString())
                if (c) {
                    holder.mContext.toast("修改成功!")
                    data.isEdit = false
                    data.fileName = file_Name.text.toString()
                    notifyItemChanged(p2)
                } else {
                    holder.mContext.toast("修改失败!")
                }
            }
        }
        val delete = holder.getView<TextView>(R.id.delete)
        delete.setOnClickListener {
            val ss = delDir(data.path)
            if (ss) {
                getData().remove(data)
                notifyDataSetChanged()
            }
        }
    }
}
